﻿Imports System.Data.SqlClient
Imports System.Drawing
Imports System.Data
Imports System.IO
Imports System.Drawing.Imaging

Partial Public Class Wfrm_Disenna_Prueba
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            ViewState("usuario") = "jquintanilla" 'Request.Form("txt_id_usuario")
            CargaInicial()

            Cargar_Preguntas()

        End If

    End Sub
    Sub CargaInicial()
        txt_Observacion_prueba1.Attributes.Add("onkeydown", "cambiaEnter()")
        txt_codigo_prueba.Text = Guid.NewGuid.ToString()
        Dim i As Integer
        For i = 1 To 21
            Dim item As New ListItem
            item.Value = i
            item.Text = i
            cmb_Seccion.Items.Add(item)
            cmb_Seccion.UpdateAfterCallBack = True
        Next
        For i = 1 To 100
            Dim item As New ListItem
            item.Value = i
            item.Text = i
            cmb_puntos.Items.Add(item)
            cmb_puntos.UpdateAfterCallBack = True


            cmb_puntos_pregunta.Items.Add(item)
            cmb_puntos_pregunta.UpdateAfterCallBack = True
        Next

        Dim csql As String = "Select * from   TB_PRUEBA_TIPO_PREGUNTA "
        Dim rp As New DotNetResponse.SQLPersistence
        'Dim Sv As New DalAppTestBuilder.TB_GENERICA
        ' Sv.GetDataTable(rp, csql)

        CMB_K_TIPO_PREGUNTA.DataSource = rp.DtTable
        CMB_K_TIPO_PREGUNTA.DataValueField = "P_TIPO_PREGUNTA"
        CMB_K_TIPO_PREGUNTA.DataTextField = "S_DESCRIPCION"
        CMB_K_TIPO_PREGUNTA.DataBind()
        CMB_K_TIPO_PREGUNTA.UpdateAfterCallBack = True
        rp.Dispose()

    End Sub
    Protected Sub Btn_Crear_Prueba_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Btn_Crear_Prueba.Click
        Dim rp As New DotNetResponse.SQLPersistence
        'Dim Sv As New DalAppTestBuilder.TB_GENERICA
        'Sv.Save(rp, Panel1, Val(txt_id_prueba.Text), txt_id_prueba.SkinID, True)

        'DalAppTestBuilder.TB_PRUEBA_DISENO.CREAR_PRUEBA_DISENNO(rp, Nothing, Val(txt_id_prueba.Text), txt_codigo_prueba.Text, cmb_Seccion.Text, txt_titulo_prueba.Text, txt_Observacion_prueba.Text, txt_usuario_prueba.Text, txt_estado_prueba1.Text, Format(Now, "yyyyMMdd hhmm"), Format(Now, "yyyyMMdd hhmm"), txt_usuario_UPDATE.Text)
        If rp.TieneDatos Then
            txt_id_prueba.Text = rp.Get_Value  '.DtTable.Rows(0).Item(0)
            txt_id_prueba.UpdateAfterCallBack = True
        End If

        If Not rp.Errores Then
            Panel1.Visible = False
            Panel2.Visible = True
            txt_fk_prueba_0.Text = txt_id_prueba.Text
            txt_usuario_seccion_c.Text = txt_usuario_UPDATE.Text
            txt_usuario_UPDATE.Text = txt_usuario_UPDATE.Text
            txt_fk_prueba_0.Text = txt_id_prueba.Text

            Panel1.UpdateAfterCallBack = True
            Panel2.UpdateAfterCallBack = True
            txt_fk_prueba_0.UpdateAfterCallBack = True
            txt_usuario_seccion_c.UpdateAfterCallBack = True
            txt_usuario_UPDATE.UpdateAfterCallBack = True
            txt_fk_prueba_0.UpdateAfterCallBack = True


            Dim i As Integer
            For i = 0 To cmb_Seccion.SelectedValue - 1
                Dim item As New ListItem
                item.Value = i + 1
                item.Text = i + 1
                cmb_Seccion.Items.Add(item)
            Next
            cmb_Seccion.UpdateAfterCallBack = True

            lbl_paso1.ForeColor = Drawing.Color.Silver
            lbl_paso1.Font.Bold = False

            lbl_paso2.ForeColor = Drawing.Color.Blue
            lbl_paso2.Font.Bold = True

            lbl_paso1.UpdateAfterCallBack = True
            lbl_paso2.UpdateAfterCallBack = True


            Dim csql As String = "Select P_SECCION,I_NRO_SECCION,S_NOMBRE_SECCION,I_PUNTOS     from   TB_PRUEBA_DISENO_SECCION where K_PRUEBA = " & txt_fk_prueba_0.Text
            'Sv.CargarGrilla(rp, Me.Grid_Secciones, csql)


        End If

    End Sub

    Protected Sub Btn_Agregar_Seccion_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Btn_Agregar_Seccion.Click
        Dim rp As New DotNetResponse.SQLPersistence
        ' Dim Sv As New DalAppTestBuilder.TB_GENERICA
        ' Sv.Save(rp, Panel2, 0, txt_id_seccion.SkinID, True)
        If rp.TieneDatos Then

            txt_id_seccion.Text = 0
            txt_titulo_seccion.Text = ""
            txt_Observacion_prueba0.Text = ""

            Dim csql As String = "Select P_SECCION,I_NRO_SECCION,S_NOMBRE_SECCION,I_PUNTOS     from   TB_PRUEBA_DISENO_SECCION where K_PRUEBA = " & txt_fk_prueba_0.Text
            '  Sv.CargarGrilla(rp, Me.Grid_Secciones, csql)


        End If



    End Sub

    Protected Sub Btn_Volver_Seccion_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Btn_Volver_Seccion.Click
        Panel1.Visible = True
        Panel2.Visible = False

        Panel1.UpdateAfterCallBack = True
        Panel2.UpdateAfterCallBack = True

    End Sub

    Protected Sub Btn_continuar_Seccion_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Btn_continuar_Seccion.Click

        Panel3.Visible = True
        Panel2.Visible = False

        Panel3.UpdateAfterCallBack = True
        Panel2.UpdateAfterCallBack = True

        lbl_paso2.ForeColor = Drawing.Color.Silver
        lbl_paso2.Font.Bold = False

        lbl_paso3.ForeColor = Drawing.Color.Blue
        lbl_paso3.Font.Bold = True

        lbl_paso2.UpdateAfterCallBack = True
        lbl_paso3.UpdateAfterCallBack = True


        'llenar secciones de la tabla, distinct 
        Dim csql As String = " SELECT   distinct [P_SECCION]"
        csql = csql & "          ,[I_NRO_SECCION]"
        csql = csql & " FROM  [dbo].[TB_PRUEBA_DISENO_SECCION]"
        csql = csql & " where K_PRUEBA =" & txt_id_prueba.Text.Trim


        Dim rp As New DotNetResponse.SQLPersistence
        'Dim Sv As New DalAppTestBuilder.TB_GENERICA
        ' Sv.GetDataTable(rp, csql)

        cmb_seccion_pregunta.DataSource = rp.DtTable
        cmb_seccion_pregunta.DataValueField = "P_SECCION"
        cmb_seccion_pregunta.DataTextField = "I_NRO_SECCION"
        cmb_seccion_pregunta.DataBind()
        cmb_seccion_pregunta.UpdateAfterCallBack = True
        rp.Dispose()


        Cargar_NroPrueba()

        Cargar_Preguntas()

    End Sub

    Protected Sub Btn_volver_preguntas_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Btn_volver_preguntas.Click
        Panel2.Visible = True
        Panel3.Visible = False

        Panel2.UpdateAfterCallBack = True
        Panel3.UpdateAfterCallBack = True

    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Btn_Agregar_Pregunta.Click
        Try


            Dim myStream As System.IO.Stream
            Dim fileLen As Integer
            Dim displayString As New StringBuilder()
            Dim bytes() As Byte
            Dim URLIMAGEN As String


            Dim chk_file As Boolean = False
            If Not File1.PostedFile Is Nothing Then
                If Not File1.PostedFile.FileName = "" Then
                    chk_file = True

                    fileLen = File1.PostedFile.ContentLength
                    Dim Input(fileLen) As Byte
                    bytes = Input

                    myStream = File1.PostedFile.InputStream  ' M_IMAGEN_PREGUNTA.PostedFile.FileContent
                    myStream.Read(Input, 0, fileLen)

                    Dim stream As New System.IO.MemoryStream()

                    Dim strTempPath As String = System.Configuration.ConfigurationManager.AppSettings("rutaimagen")
                    Dim strFileName As String = Guid.NewGuid.ToString("N") + ".img"
                    Dim fs As New FileStream(strTempPath + "\" + strFileName, FileMode.OpenOrCreate, FileAccess.Write)
                    Dim offset As Integer = 0
                    fs.Write(Input, offset, Input.Length - offset)
                    fs.Close()
                    fs = Nothing

                    URLIMAGEN = "./UploadImagenes/" & strFileName

                    'Image1.ImageUrl = System.Configuration.ConfigurationManager.AppSettings("rutalogicaimagen") & strFileName
                    'Image1.UpdateAfterCallBack = True
                End If
            End If


            Dim Rp As New DotNetResponse.SQLPersistence
            Dim exec As New DotNetDal.SQLPersistence(True, "cnn", DotNetDal.SQLPersistence.Provider.SQLCLIENT)

            Dim texto As String = ""
            'Dim i As Integer
            'For i = 0 To txt_Observacion_prueba1.Text.Split(".-").Length - 1
            '    texto = texto & txt_Observacion_prueba1.Text.Split(".-")(i) & "</br> "
            'Next
            texto = txt_Observacion_prueba1.Text.Replace(".-", ".-<br>")
            Rp.AddParametro(0)
            Rp.AddParametro(cmb_seccion_pregunta.SelectedValue)
            Rp.AddParametro(HttpUtility.HtmlDecode(texto))
            Rp.AddParametro(bytes)
            Rp.AddParametro(chk_file)
            Rp.AddParametro(cmb_puntos_pregunta.SelectedValue)
            Rp.AddParametro("V")
            Rp.AddParametro(lbl_I_NRO_PREGUNTA.Text)
            Rp.AddParametro(ViewState("usuario"))
            Rp.AddParametro(ViewState("usuario"))
            Rp.AddParametro(Format(Now, "yyyyMMdd hhmm"))
            Rp.AddParametro(Format(Now, "yyyyMMdd hhmm"))
            Rp.AddParametro(CMB_K_TIPO_PREGUNTA.SelectedValue)
            Rp.AddParametro(chk_file)
            Rp.AddParametro("")
            Rp.AddParametro(URLIMAGEN.ToString)
            Rp.StoredProcedure = "SP_CREAR_PREGUNTA_DISENNO"

            exec.ExecuteDataTable(Rp)
            exec.Commit()
            exec.Dispose()

            If Not Rp.Errores Then
                lbl_I_NRO_PREGUNTA.Text = Val(lbl_I_NRO_PREGUNTA.Text) + 1

                Cargar_Preguntas()
            End If

        Catch ex As Exception


        End Try



    End Sub
    Sub Cargar_Preguntas()
        Dim csql As String = " SELECT     TB_PRUEBA_DISENO_SECCION.I_NRO_SECCION, TB_PRUEBA_DISENO_PREGUNTA.P_PREGUNTA, "
        csql = csql & " TB_PRUEBA_DISENO_PREGUNTA.I_NRO_PREGUNTA, TB_PRUEBA_DISENO_PREGUNTA.I_PUNTOS_PREGUNTA, TB_PRUEBA_DISENO_PREGUNTA.K_TIPO_PREGUNTA "
        csql = csql & " FROM         TB_PRUEBA_DISENO INNER JOIN "
        csql = csql & " TB_PRUEBA_DISENO_SECCION ON TB_PRUEBA_DISENO.P_PRUEBA = TB_PRUEBA_DISENO_SECCION.K_PRUEBA INNER JOIN "
        csql = csql & " TB_PRUEBA_DISENO_PREGUNTA ON TB_PRUEBA_DISENO_SECCION.P_SECCION = TB_PRUEBA_DISENO_PREGUNTA.K_SECCION "
        csql = csql & " where p_prueba =" & txt_id_prueba.Text
        csql = csql & " order by I_NRO_SECCION, I_NRO_PREGUNTA "

        Dim rp As New DotNetResponse.SQLPersistence
        'Dim Sv As New DalAppTestBuilder.TB_GENERICA
        ' Sv.CargarGrilla(rp, Grid_Preguntas, csql)


    End Sub

    Protected Sub cmb_seccion_pregunta_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles cmb_seccion_pregunta.SelectedIndexChanged
        'Buscar el mayor de las preguntas por la sección
        Cargar_NroPrueba()
    End Sub
    Sub Cargar_NroPrueba()
        Dim csql As String = " select isnull(MAX(I_NRO_PREGUNTA ),0)+1 from TB_PRUEBA_DISENO_PREGUNTA where K_SECCION =" & cmb_seccion_pregunta.SelectedValue
        Dim rp As New DotNetResponse.SQLPersistence
        'Dim Sv As New DalAppTestBuilder.TB_GENERICA
        ' Sv.GetDataTable(rp, csql)

        lbl_I_NRO_PREGUNTA.Text = rp.DtTable.Rows(0).Item(0)
        lbl_I_NRO_PREGUNTA.UpdateAfterCallBack = True

    End Sub

    Protected Sub Grid_Preguntas_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles Grid_Preguntas.SelectedIndexChanged
        Dim titulo As String
        Dim pagina As String
        Dim parametros As String = "?usuario=" & ViewState("usuario") & "&K_TIPO_PREGUNTA=" & Grid_Preguntas.SelectedItem.Cells(6).Text & "&K_PREGUNTA=" & Grid_Preguntas.SelectedItem.Cells(3).Text
        Select Case Grid_Preguntas.SelectedItem.Cells(6).Text
            Case 1
                pagina = "WFrm_Alternativa_Seleccion_Multiple.aspx"
            Case 2
                pagina = ""
            Case 3
                pagina = "WFrm_Alternativa_Seleccion_Unica_SinImagen.aspx"
            Case 4

            Case 5
            Case 6
            Case 7
            Case 8
                pagina = "WFrm_Alternativa_Seleccion_Unica_ConImagen.aspx"
            Case 9
            Case 10
            Case 11


        End Select
        Dim script As String = "  myLightWindow.activateWindow({"
        script = script & " href:   '" & pagina & parametros & "', "
        script = script & " title:  '" & Titulo & "', "
        script = script & " author:  'JQUINTANILLA', "
        script = script & " caption:  '', "
        script = script & "  left: 500,"
        script = script & "  width: 600,"
        script = script & "  height: 400,"
        script = script & "  top: 300"
        script = script & " });"

        Anthem.Manager.AddScriptForClientSideEval(script)

    End Sub
End Class